﻿Public Class frmMeetingsInsert

    Dim strNameForm As String = "Insert Meeting"
    Public mId As Integer

    Public Function Initialize() As Boolean
        Try
            Me.Text = strNameForm

            cboTopics.Load("Select * From Topics", "Name", gConnectionStringAgenda.ToString)
            cboUS.Load("Select * From UserStories", "Name", gConnectionStringAgenda.ToString)
            Dim listStatus As New List(Of Base.GlobalConst)
            listStatus.Add(MEETING_STATUS_PLANNED)
            listStatus.Add(MEETING_STATUS_DONE)
            listStatus.Add(MEETING_STATUS_CANCELLED)
            cboStatus.DataSource = listStatus
            cboStatus.ValueMember = "NUMBER"
            cboStatus.DisplayMember = "NAME"
            listStatus = Nothing

            AddHandler cboTopics.OnInsert, AddressOf InterfaceManager.Topics_OnInsert
            AddHandler cboTopics.OnModify, AddressOf InterfaceManager.Topics_OnModify
            AddHandler cboUS.OnInsert, AddressOf InterfaceManager.UserStories_OnInsert
            AddHandler cboUS.OnModify, AddressOf InterfaceManager.UserStories_OnModify

            Return True
        Catch ex As Exception
            Base.ShowError.Show("Initialize", Me.Name, "Initialize", ex)
            Return False
        End Try
    End Function

#Region "Commands"

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub cmdInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInsert.Click
        Try
            If IsComplete() Then
                Dim oTMeetings As New Data.TMeetings
                oTMeetings.mName = txtName.Text
                oTMeetings.mDescription = txtDescription.Text
                oTMeetings.mDate = txtDate.DataMySql
                oTMeetings.mTime = txtTime.Time
                oTMeetings.mTopicsID = cboTopics.SelectedValue
                oTMeetings.mUserStoriesID = cboUS.SelectedValue
                oTMeetings.mStatus = cboStatus.SelectedValue

                mId = oTMeetings.Insert
                If mId > 0 Then
                    Me.DialogResult = Windows.Forms.DialogResult.OK
                Else
                    MsgBox("Impossible insert the meeting.", vbExclamation, Me.Text)
                    Me.DialogResult = Windows.Forms.DialogResult.Cancel
                End If
                oTMeetings = Nothing
            End If
        Catch ex As Exception
            Base.ShowError.Show("Insertion", Me.Name, "cmdInsert_Click", ex)
        End Try
    End Sub

#End Region

#Region "Other functions"

    Private Function IsComplete() As Boolean
        Try
            If txtDescription.Text = "" Then
                MsgBox("Insert description.", vbExclamation, Me.Name)
                txtDescription.Focus()
                Return False
            End If
            If txtDate.Data = "" Then
                MsgBox("Select date.", vbExclamation, Me.Name)
                txtDate.Focus()
                Return False
            End If
            If txtTime.Time = "" Then
                MsgBox("Select time.", vbExclamation, Me.Name)
                txtDate.Focus()
                Return False
            End If

            Return True
        Catch ex As Exception
            Base.ShowError.Show("Insertion", Me.Name, "IsComplete", ex)
            Return False
        End Try
    End Function

#End Region

#Region "Form events"

    Private Sub me_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = DialogResult.Cancel
        End If
    End Sub

#End Region

End Class